package com.geekstudy.week3.service;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 配置 MicroProfile Rest Client
 * https://www.ibm.com/docs/zh/was-liberty/nd?topic=liberty-configuring-microprofile-rest-client
 *
 * MicroProfile REST Client
 * https://mvnrepository.com/artifact/org.eclipse.microprofile.rest.client/microprofile-rest-client-api/2.0
 *
 * Javax WS RS API
 * https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api/2.1.1
 *
 * https://projects.eclipse.org/projects/technology.microprofile
 * https://projects.eclipse.org/projects/technology.microprofile/developer
 * https://github.com/eclipse/microprofile-rest-client
 * % git clone git@github.com:eclipse/microprofile-rest-client
 *
 * MicroProfile 实现
 * Apache CXF (http://cxf.apache.org/download.html)
 * Open Liberty (https://openliberty.io/blog/2018/01/31/mpRestClient.html)
 * Thorntail (https://github.com/thorntail/thorntail/tree/master/fractions/microprofile/microprofile-restclient)
 * RESTEasy (https://resteasy.github.io)
 *     https://resteasy.github.io/downloads/
 *     https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs/3.15.1.Final
 * Jersey (https://github.com/eclipse-ee4j/jersey)
 *
 *
 * http://cxf.apache.org/docs/restful-services.html
 * http://cxf.apache.org/docs/rest-with-jax-ws-provider-and-dispatch.html
 * http://cxf.apache.org/docs/jax-rs.html
 * http://cxf.apache.org/docs/advanced-integration.html
 *
 */
@Service
public class MicroProfileRestClientService {

    final Logger logger = LoggerFactory.getLogger(MicroProfileRestClientService.class);

    public Map<String, Map<String, String>> postShutdown() {
        // 在您编写接口和响应异常映射器之后，请构建实现并将其启动。
        // 可以使用 RestClientBuilder API 或上下文和依赖性注入 (CDI) 和“MP 配置”来构建实现。

        // 6. 可选： 使用 CDI 和“MP 配置”实例化客户机。
        // 在 Libertyserver.xml 文件中安装 mpRestClient-1.0 或 mpRestClient-1.1 功能部件。
        // 添加 cdi-1.2 或 cdi-2.0 功能部件。
        // 添加 mpConfig-1.1、mpConfig-1.2 或 mpConfig-1.3 功能部件。
        // 使用以下注释更新 MusicPlaylistService 接口：

        Map<String, Map<String, String>> map = new LinkedHashMap<String, Map<String, String>>();
        try {
            //String shutdownUrl = "http://127.0.0.1:9103/mock/actuator/shutdown?from=202107";
            //MockShutdownApi mockShutdown = RestClientBuilder.newBuilder().baseUri(new URI(shutdownUrl)).build(MockShutdownApi.class);
            //Map<String, String> mockMap = mockShutdown.shutdown("abcd");
            //logger.info("MicroProfile postShutdown map:{} ", mockMap);
            //map.put("restClient", mockMap);

            String serverShutdownUrl = "http://127.0.0.1:9103/mock/actuator/shutdown?from=2021demo";
            RestTemplate restTemplate = new RestTemplate();
            Map<String, String> serverShutdownMap = restTemplate.postForObject(serverShutdownUrl, null, Map.class);
            logger.info("request server shutdown serverShutdownMap:{} ", serverShutdownMap);
            map.put("serverShutdown", serverShutdownMap);

            String mockShutdownUrl = "http://127.0.0.1:9103/jersey/demo?demo=shutdown2021";
            // String mockShutdownUrl = "http://127.0.0.1:9103/mock/shutdown?from=shutdown2021";
            Map<String, String> mockShutdownMap = restTemplate.postForObject(mockShutdownUrl, null, Map.class);
            logger.info("request mock shutdown mockShutdownMap:{} ", mockShutdownMap);
            map.put("mockShutdown", mockShutdownMap);

        } catch(Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
